//
//  LCFilesListVC.m
//  LongChain
//
//  Created by FengZi on 2018/4/24.
//  Copyright © 2018年 FengZi. All rights reserved.
//

#import "LCFilesListVC.h"
#import "LCFilesListCell.h"
#import "LCFileModel.h"
#import <Masonry/Masonry.h>
@interface LCFilesListVC ()<UICollectionViewDelegateFlowLayout,UICollectionViewDelegate,UICollectionViewDataSource>

@property (weak, nonatomic) IBOutlet UICollectionView *filesList;

@property (nonatomic, assign) NSInteger filePage;

@property (nonatomic, strong) NSArray <LCFileModel *>*fileDatas;

@property (weak, nonatomic) IBOutlet UIView *withOutDataBaseView;
@property (weak, nonatomic) IBOutlet UIImageView *withOutDataImage;
@property (weak, nonatomic) IBOutlet UILabel *withOutDataDes;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *bottomViewheight;

@property (nonatomic, copy) NSString *fileID;

@property (nonatomic, copy) NSString *url;

@property (nonatomic, assign) BOOL listState;

@property (nonatomic, strong) NSMutableArray *selectedFiles;

@end

@implementation LCFilesListVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
 
    
    WS(weakSelf);
    [self navigationTitle];
    self.filesList.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        weakSelf.filePage = 1;
        weakSelf.fileDatas = [NSArray new];
        [weakSelf loadFileDatas];

    }];
    self.filesList.mj_footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
        [weakSelf loadFileDatas];

    }];
    [self.filesList.mj_header beginRefreshing];
    
    self.selectedFiles = [NSMutableArray arrayWithCapacity:0];
    
}

- (IBAction)fileAction:(UIButton *)sender {
    switch (sender.tag) {
        case 10:  //  保存
        {
            [self showHint:@"保存"];
        }
            break;
        case 20:  //  删除
        {
            [self showHint:@"删除"];

        }
            break;
        case 30:  //  编辑标签
        {
            [self showHint:@"编辑标签"];

        }
            break;
            
        default:
            break;
    }
}

#pragma mark - - UICollectionViewDataSource UICollectionViewDelegate

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    NSInteger row = indexPath.row;
    WS(weakSelf);
    LCFilesListCell *filesListCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"fileCell" forIndexPath:indexPath];
    if (self.fileDatas.count > row) {
        [filesListCell lc_updateFilesListCellWithParameter:self.fileDatas[row] enableSelected:self.listState];
    }
    filesListCell.selectedHandler = ^(LCFileModel *mode) {
        [weakSelf fileActionWithModel:mode];
    };
    return filesListCell;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return self.fileDatas.count;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    return CGSizeMake((SWidth - 55) / 4, (SWidth - 55) / 4);
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {

    NSInteger row = indexPath.row;
    self.fileID = @"";
    self.url = @"";
    if (self.fileDatas.count > row ) {
        LCFileModel *model = self.fileDatas[row];
        NSString *fileID = [NSString stringWithFormat:@"%@",model.fileID];
        NSString *url = [NSString stringWithFormat:@"%@",model.fileURL];
        self.fileID = fileID;
        self.url = url;
        [self performSegueWithIdentifier:@"showFileDetail" sender:self];
        

    }
//    [self performSegueWithIdentifier:@"showFileDetail" sender:self];
}

#pragma mark - -

- (void)navigationTitle {
    switch (self.fileType) {
        case FileTypeAll:
        {
            self.title = @"全部";
        }
            break;
        case FileTypeMusic:
        {
            self.title = @"音乐";
        }
            break;
        case FileTypePhoto:
        {
            self.title = @"照片";
        }
            break;
        case FileTypeVideo:
        {
            self.title = @"视频";
        }
            break;
        case FileTypeDocument:
        {
            self.title = @"文档";
        }
            break;
            
        default:
            break;
    }
}
- (void)loadFileDatas {
    
    WS(weakSelf);
    NSString *requestURL = @"";
    if (self.fileType == FileTypeAll) {
        //  全部文件
        requestURL = [NSString stringWithFormat:@"/api/v0/nas/file?dir_id=0"];
        
    }else if(self.fileType == FileTypeTag){
        //  标签列表
        requestURL = [NSString stringWithFormat:@"/api/v0/nas/tag/file/search?tag_id=%@&page=%ld&pageSize=4",self.tagID,(long)self.filePage];
        
    }else{
        requestURL = [NSString stringWithFormat:@"/api/v0/nas/file/list?role=%ld&page=%ld&pageSize=10",(long)self.fileType,(long)self.filePage];
        
    }
    
    [[LCNetwork shareInstance]lc_getNetworkWithURL:requestURL responseSuccess:^(id resultCode, id resultParameter) {
        
        LCLog(@"resultParameter = %@",resultParameter);
        [weakSelf.filesList.mj_header endRefreshing];
        [weakSelf.filesList.mj_footer endRefreshing];
        
        weakSelf.filePage ++;
        if ([resultCode isEqualToString:@"0"]) {
            NSDictionary *data = resultParameter[@"data"];
            NSArray *files = data[@"files"];
            if (files.count > 0) {
                NSMutableArray *temp = [NSMutableArray arrayWithArray:self.fileDatas];
                [files enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                    LCFileModel *model = [LCFileModel configModelWithParameter:obj state:NO];
                    [temp addObject:model];
                }];
                weakSelf.fileDatas = temp;
                [weakSelf showOrHiddenWithoutView:(self.fileDatas.count == 0)];
                [weakSelf.filesList reloadData];
                
            }else{
                [weakSelf.filesList.mj_footer endRefreshingWithNoMoreData];
            }
        }
        
    } responseFail:^(id resultCode, NSError *error) {
        
    }];
}

- (void)showOrHiddenWithoutView:(BOOL)show {
    
    self.withOutDataBaseView.hidden = !show;
    self.filesList.hidden = show;
    NSString *imageName = @"";
    NSString *des = @"";
    switch (self.fileType) {
        case FileTypeAll:
        {
            imageName = @"list_img_allnothing";
            des = @"这里暂无文件呦!";
        }
            break;
        case FileTypeDocument:
        {
            imageName = @"list_img_document_nothing";
            des = @"这里暂无文档呦!";
            
        }
            break;
        case FileTypeVideo:
        {
            imageName = @"list_img_video_nothing";
            des = @"还没有影视视频呦!";
            
        }
            break;
        case FileTypePhoto:
        {
            imageName = @"list_img_pic_nothing";
            des = @"这里暂无图片呦!";
            
        }
            break;
        case FileTypeMusic:
        {
            imageName = @"list_img_music_nothing";
            des = @"这里暂无音乐呦!";
            
        }
            break;
            
        default:
            break;
    }
    self.withOutDataImage.image = [UIImage imageNamed:imageName];
    self.withOutDataDes.text = des;
    
}
- (IBAction)chooseField:(UIBarButtonItem *)sender {
    NSString *title = self.listState ? @"选择" : @"取消";
    sender.title = title;
    CGFloat bottomHeight = self.listState ? 0 : 50;
    self.bottomViewheight.constant = bottomHeight;
    [self.filesList reloadData];
    self.listState = !self.listState;

}

- (void)fileActionWithModel:(LCFileModel *)model {
    
    if (![self.selectedFiles containsObject:model.fileID]) {
        [self.selectedFiles addObject:model.fileID];
    }else{
        [self.selectedFiles removeObject:model.fileID];
    }
    LCLog(@"selectedFiles = %@",self.selectedFiles);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
    
    if ([segue.identifier isEqualToString:@"showFileDetail"]) {
        id vc = segue.destinationViewController;
        [vc setValue:self.url forKey:@"imageURL"];
        [vc setValue:self.fileID forKey:@"file_id"];
    }
}


@end
